/*
* @(#)MaintDmsCompoundDocumentAction.java
*
* Copyright (c) 2003 DCIVision Ltd
* All rights reserved.
*
* This software is the confidential and proprietary information of DCIVision
* Ltd ("Confidential Information").  You shall not disclose such Confidential
* Information and shall use it only in accordance with the terms of the license
* agreement you entered into with DCIVision Ltd.
 */
package com.dcivision.dms.web;

import java.io.InputStream;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.upload.FormFile;

import com.dcivision.dms.DmsOperationConstant;
import com.dcivision.dms.bean.DmsDocument;
import com.dcivision.dms.bean.DmsRoot;
import com.dcivision.dms.bean.DmsValidation;
import com.dcivision.dms.core.DocumentOperationManager;
import com.dcivision.dms.core.DocumentRetrievalManager;
import com.dcivision.dms.core.DocumentValidateManager;
import com.dcivision.dms.core.RootRetrievalManager;
import com.dcivision.dms.dao.DmsDocumentDAObject;
import com.dcivision.framework.ApplicationContainer;
import com.dcivision.framework.ApplicationException;
import com.dcivision.framework.GlobalConstant;
import com.dcivision.framework.PermissionManager;
import com.dcivision.framework.SessionContainer;
import com.dcivision.framework.SystemFunctionConstant;
import com.dcivision.framework.TextUtility;
import com.dcivision.framework.Utility;
import com.dcivision.framework.web.AbstractActionForm;

/**
  MaintDmsDocumentAction.java

  This class is for maint dms dmsDocument action

    @author          Zoe Shum
    @company         DCIVision Limited
    @creation date   06/08/2003
    @version         $Revision: 1.42.2.3 $
 */

public class MaintDmsCompoundDocumentAction extends MaintDmsDocumentAction {

  public static final String REVISION = "$Revision: 1.42.2.3 $";

  protected String sListName = null;

  public MaintDmsCompoundDocumentAction() {
    super();
    sListName = "dmsCompoundDocumentList";
  }

  /**
   * getMajorDAOClassName
   *
   * @return  The class name of the major DAObject will be used in this action.
   */
  public String getMajorDAOClassName() {
    return("com.dcivision.dms.dao.DmsDocumentDAObject");
  }

  /**
   * getFunctionCode
   *
   * @return  The corresponding system function code of action.
   */
  public String getFunctionCode() {
    return(SystemFunctionConstant.DMS_PUBLIC_FOLDER);
  }

  /**
   * execute - Override the parent function.
   *
   * @param mapping
   * @param form
   * @param request
   * @param response
   * @return
   * @throws ServletException
   */
  public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws ServletException {

    MaintDmsDocumentForm actionForm = (MaintDmsDocumentForm) form;
    // Set function code if necessary.
    if (!Utility.isEmpty(actionForm.getCurFunctionCode())) {
      this.setFunctionCode(actionForm.getCurFunctionCode());
    }else if(!Utility.isEmpty(request.getParameter("sCurFunctionCode"))){
      this.setFunctionCode(request.getParameter("sCurFunctionCode"));
    }
    if( !Utility.isEmpty(functionCode)) {
      request.setAttribute(GlobalConstant.FUNCTION_CODE_KEY,functionCode);
    } else {
      request.setAttribute(GlobalConstant.FUNCTION_CODE_KEY,this.getFunctionCode());
    }

    String opMode = actionForm.getOpMode();
    String navMode = actionForm.getNavMode();
   
    SessionContainer sessionContainer = this.getSessionContainer(request);
    Connection conn = this.getConnection(request);
    RootRetrievalManager rootRetrievalManager = new RootRetrievalManager(sessionContainer, conn);
    PermissionManager permissionManager=sessionContainer.getPermissionManager();

    ActionForward forward = this.retrieveFunctionCode(request, response, mapping);
    if (forward!=null) {
      return forward;
    }

    // Retrieve the default document profile settings.
    DocumentRetrievalManager docRetrievalManager = new DocumentRetrievalManager(sessionContainer, conn);
    docRetrievalManager.checkNSetDefaultProfileList(request);

    // Get the location path.
    parentID = actionForm.getParentID();
    rootID = actionForm.getRootID();
    docType = actionForm.getDocumentType();

    if (!GlobalConstant.NAV_MODE_VIEW.equals(navMode)){
      selectChildList(TextUtility.parseIntegerObj(actionForm.getID()), TextUtility.parseIntegerObj(actionForm.getRootID()), request,form);
      /* Retrieval the document information for display in JSP */
      
      if("ListDmsCompoundDocument".equals(actionForm.getShowTree())){
        request.setAttribute("showTree","ListDmsCompoundDocument");
      }
      

      // Retrieve the accessible public root list.
      List publicRootList = rootRetrievalManager.getAllAccessibleRootsList();
      request.setAttribute("publicRootList", publicRootList);
      
      // Retrieve the root folder document object.
      DmsDocument documentRoot = docRetrievalManager.getRootFolderByRootID(TextUtility.parseIntegerObj(actionForm.getRootID()));
      //Check for null parentID rootID hanling
      if ((Utility.isEmpty(parentID) || "null".equals(parentID) || Utility.isEmpty(rootID) || "null".equals(rootID)) && permissionManager.getPersonalPermission().indexOf('I') > -1) {
        DmsRoot personalRoot = rootRetrievalManager.getRootObjectByOwnerID(sessionContainer.getUserRecordID());
        documentRoot = docRetrievalManager.getRootFolderByRootID(personalRoot.getID());
        parentID = TextUtility.formatIntegerObj(documentRoot.getID());
        rootID = TextUtility.formatIntegerObj(personalRoot.getID());
        actionForm.setRootID(rootID);
        actionForm.setParentID(parentID);
      } else if ((Utility.isEmpty(parentID) || "null".equals(parentID) || Utility.isEmpty(rootID) || "null".equals(rootID)) && !Utility.isEmpty(publicRootList)) {
      	DmsRoot publicRoot = (DmsRoot)publicRootList.get(0);
      	documentRoot = docRetrievalManager.getRootFolderByRootID(publicRoot.getID());
        parentID = TextUtility.formatIntegerObj(documentRoot.getID());
        rootID = TextUtility.formatIntegerObj(publicRoot.getID());
        actionForm.setRootID(rootID);
        actionForm.setParentID(parentID);
      }
      request.setAttribute("rootFolder", documentRoot);
      
      //in the maintDmsCheckoutAction.java set the returnTo parameter
      if(Utility.isEmpty(actionForm.getReturnTo())){
        actionForm.setReturnTo((String)request.getAttribute("returnTo"));
      }

      String path = "";
      if (parentID != null) {
        path = docRetrievalManager.getLocationPath(TextUtility.parseIntegerObj(parentID));
      }
      request.setAttribute("location", path);

      // Retrieve the folder list for folder tree listing.
      List folderTreeList = docRetrievalManager.getFolderByParentIDRootID(documentRoot.getID(), TextUtility.parseIntegerObj(actionForm.getRootID()));
      request.setAttribute("folderTreeList", folderTreeList);
      /* END of JSP Retrieval */
    }

    if (DmsOperationConstant.CREATE_COMPOUND_DOCUMENT.equals(navMode)){
      super.init(mapping, actionForm, request, response);      
      if (GlobalConstant.NAV_MODE_REFRESH.equals(opMode)) {
        log.debug("refreshing ...");

        try {
          refreshFormData(mapping, actionForm, request, response);
        } catch (ApplicationException appEx) {
          this.rollback(request);
          handleApplicationException(request, appEx);
        }
        return mapping.findForward(DmsOperationConstant.CREATE_COMPOUND_DOCUMENT);
      }else{
        return mapping.findForward(DmsOperationConstant.CREATE_COMPOUND_DOCUMENT);
      }
    }
    
    if ("returnTop".equals(navMode)){
      return mapping.findForward(DmsOperationConstant.CREATE_COMPOUND_DOCUMENT_VIEW);
    }
    

    if (GlobalConstant.NAV_MODE_VIEW.equals(navMode)){
      selectChildList(TextUtility.parseIntegerObj(actionForm.getID()), TextUtility.parseIntegerObj(actionForm.getRootID()), request,form);
    }

    sessionContainer = null;
    conn=null;
    docRetrievalManager.release();
    
    //Create Compound Document
    if (GlobalConstant.OP_MODE_INSERT.equals(opMode)){
      // Insert operation.
      log.debug("enter insert Compound Document");
      try {
        this.insertRecord(mapping, actionForm, request, response);
        this.handleWorkflowRoutine(mapping, actionForm, request, response, opMode, navMode);           
        this.commit(request);
        addMessage(request, MESSAGE_RECORD_INSERT);
      } catch (ApplicationException appEx) {
        this.rollback(request);
        handleApplicationException(request, appEx);
      }
      if (isError(request)) {
      	actionForm.setNavMode(null);
      	actionForm.setOpMode(null);
//        actionForm.setNavMode(GlobalConstant.NAV_MODE_REFRESH);
        this.refreshFormData(mapping, actionForm, request, response);
        return mapping.findForward(GlobalConstant.NAV_MODE_NEW);
      }
      // if band workflow ,then return list page
      else if(!Utility.isEmpty(request.getParameter("workflowRecordID"))&&
                  !"null".equals(request.getParameter("nextStepDueDates"))){
        actionForm.setNavMode(GlobalConstant.NAV_MODE_VIEW);
        return mapping.findForward(GlobalConstant.NAV_MODE_EDIT);          
      }      
      else {
        actionForm.setNavMode(GlobalConstant.NAV_MODE_VIEW);
        return mapping.findForward(DmsOperationConstant.CREATE_COMPOUND_DOCUMENT_VIEW);
      }
    }else if(GlobalConstant.OP_MODE_LIST_DELETE.equals(opMode)){
      log.debug("enter delete list docuument");
      try {
        this.deleteListData2(mapping, actionForm, request, response);
        this.commit(request);
        //addMessage(request, MESSAGE_RECORD_DELETE); EIP-1198
        actionForm.setNavMode(GlobalConstant.NAV_MODE_VIEW);

        selectChildList(TextUtility.parseIntegerObj(actionForm.getID()), TextUtility.parseIntegerObj(actionForm.getRootID()), request,form);
      } catch (ApplicationException appEx) {
        this.rollback(request);
        handleApplicationException(request, appEx);
      }
      if (isError(request)) {
        actionForm.setNavMode(GlobalConstant.NAV_MODE_VIEW);
        this.refreshFormData(mapping, actionForm, request, response);
        return mapping.findForward(GlobalConstant.NAV_MODE_VIEW);
      } else {
        actionForm.setNavMode(GlobalConstant.NAV_MODE_VIEW);
        return mapping.findForward(GlobalConstant.NAV_MODE_VIEW);
      }
    }else if(GlobalConstant.OP_MODE_UPDATE.equals(opMode)){
      try{
        super.updateRecord(mapping, actionForm, request, response);
        selectChildList(TextUtility.parseIntegerObj(actionForm.getID()), TextUtility.parseIntegerObj(actionForm.getRootID()), request,form);
        this.commit(request);
      } catch (ApplicationException appEx) {
        this.rollback(request);
        handleApplicationException(request, appEx);
      }
      if (isError(request)) {
      	actionForm.setNavMode(null);
      	actionForm.setOpMode(null);
//        actionForm.setNavMode(GlobalConstant.NAV_MODE_VIEW);
        this.refreshFormData(mapping, actionForm, request, response);
        return mapping.findForward(GlobalConstant.NAV_MODE_VIEW);
      } else {
        actionForm.setNavMode(GlobalConstant.NAV_MODE_VIEW);
        return mapping.findForward(GlobalConstant.NAV_MODE_VIEW);
      }
    }
    //Refresh mode
    else if (GlobalConstant.NAV_MODE_REFRESH.equals(opMode)) {
      log.debug("refreshing ...");
      
      String docID = (String)request.getAttribute("ID");
      String docNavMode = (String)request.getAttribute("navMode");
      if (Utility.isEmpty(docNavMode)){
        docNavMode = actionForm.getNavMode();
      }
        actionForm.setID(docID);
      actionForm.setNavMode(docNavMode);      
      if(Utility.isEmpty(navMode)){
        navMode=GlobalConstant.NAV_MODE_VIEW;
      }
      try {
        refreshFormData(mapping, actionForm, request, response);
      } catch (ApplicationException appEx) {
        this.rollback(request);
        handleApplicationException(request, appEx);
      }
      if (isError(request)) {
        actionForm.setNavMode(navMode);
        return mapping.findForward(navMode);
      } else {
        return mapping.findForward(navMode);
      }
    }else if(DmsOperationConstant.CHECKOUT_OPERATION.equals(opMode)){
      log.debug("refreshing ...");

      try {
        refreshFormData(mapping, actionForm, request, response);
      } catch (ApplicationException appEx) {
        this.rollback(request);
        handleApplicationException(request, appEx);
      }
      if (isError(request)) {
        actionForm.setNavMode(GlobalConstant.NAV_MODE_VIEW);
        return mapping.findForward(GlobalConstant.NAV_MODE_VIEW);
      } else {
        return mapping.findForward(GlobalConstant.NAV_MODE_VIEW);
      }
    }else { // Else Case get from super class
     return super.execute(mapping, actionForm, request, response);
    }
  }

  /**
   * this function is to getting the supporting documents of compound document.
   * @param ID                          ID of Compound document
   * @param rootID                      RootID of Compound document
   * @param request                     HttpServletRequest
   * @param form                        ActionForm of struts
   */
  public void selectChildList(Integer ID, Integer rootID,HttpServletRequest request,ActionForm form) throws ApplicationException{
    MaintDmsDocumentForm maintForm = (MaintDmsDocumentForm) form;
    SessionContainer sessionContainer = this.getSessionContainer(request);
    Connection conn = this.getConnection(request);
    DocumentRetrievalManager docRetrievalManager = new DocumentRetrievalManager(sessionContainer, conn);
    List documentList = new ArrayList();
    List newDocumentList=new ArrayList();
    int startOffset = TextUtility.parseInteger(maintForm.getCurStartRowNo());
    int pageSize = TextUtility.parseInteger(maintForm.getPageOffset());
    int rowLoopCnt = 0;
    try{
      documentList = docRetrievalManager.getDocumentListByParentIDRootID(ID, rootID);
    } catch (Exception ee) {
      log.error(ee,ee);
    }
    //reset the value of dmsDocument for paging.
    if (!Utility.isEmpty(documentList)){
      if(documentList.size()-startOffset>=pageSize){
        for(int i=startOffset-1;i<startOffset-1+pageSize;i++){
          DmsDocument dmsDocument=(DmsDocument)documentList.get(i);
          dmsDocument.setRowNum(i+1);
          dmsDocument.setRecordCount(documentList.size());
          dmsDocument.setHasRelationship(docRetrievalManager.hasRelationship(dmsDocument.getID(),""));
          newDocumentList.add(dmsDocument);
        }
      }else{
        for(int i=startOffset-1;i<documentList.size();i++){
          DmsDocument dmsDocument=(DmsDocument)documentList.get(i);
          dmsDocument.setRowNum(i+1);
          dmsDocument.setRecordCount(documentList.size());
          dmsDocument.setHasRelationship(docRetrievalManager.hasRelationship(dmsDocument.getID(),""));
          newDocumentList.add(dmsDocument);    
        }
      }
    }
    
    request.setAttribute(sListName, newDocumentList);
    
    List resultList = (List)request.getAttribute(sListName);
    while ( (resultList.size()==0) && (TextUtility.parseInteger(maintForm.getCurStartRowNo())>TextUtility.parseInteger(maintForm.getPageOffset())) ) {
      int prvRowNo = TextUtility.parseInteger(maintForm.getCurStartRowNo())-TextUtility.parseInteger(maintForm.pageOffset);
      maintForm.setCurStartRowNo(TextUtility.formatInteger(prvRowNo));
      selectChildList(ID, rootID, request,form);
    }
  }
  
  /**
   * execute - Override the parent function.
   *
   * @param mapping
   * @param form
   * @param request
   * @param response
   * @return
   * @throws ServletException
   */
  public void insertRecord(ActionMapping mapping, AbstractActionForm form, HttpServletRequest request, HttpServletResponse response) throws ApplicationException {
    boolean createFile = true;
    InputStream inputStream = null;
    FormFile formFile = null;

    MaintDmsDocumentForm documentForm = (MaintDmsDocumentForm) form;
    DmsDocument newDocument = null;
    SessionContainer sessionContainer = this.getSessionContainer(request);
    Connection conn = this.getConnection(request);
    DocumentRetrievalManager docRetrievalManager = new DocumentRetrievalManager(sessionContainer, conn);
    DocumentOperationManager docOperationManager = new DocumentOperationManager(sessionContainer, conn);

    try {
      //Create Document
      formFile = documentForm.getUploadFile();
      if (formFile !=null) {
        inputStream = formFile.getInputStream();
        log.debug("target document size: " + formFile.getFileSize());
      }
    } catch (Exception ee) {
      log.error(ee, ee);
    } 
    DmsDocument dmsDocument = (DmsDocument) documentForm.getFormData();

    boolean createPhysicalFile =documentForm.getCreatePhysicalFile();

    //if (formFile !=null){
      super.insertRecord(mapping, form, request, response);
      
    sessionContainer = null;
    conn=null;
    docRetrievalManager.release();
    docOperationManager.release();
  }

  /**
 *  Method deleteListData() - delete the item(s) from listing page
 *
 *  @param      mapping               ActionMapping from struts
 *  @param      form                  ActionForm from struts
 *  @param      request               HttpServletReuqest
 *  @param      respond               HttpServletRespond
 *  @return     void
 *  @throws     ApplicationException  Throws ApplicationException the deleteion failed
 */
  public void deleteListData(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws ApplicationException {
    MaintDmsDocumentForm searchForm = (MaintDmsDocumentForm)form;
    String[] idAry = request.getParameterValues("basicSelectedID");

    // String[] idAry = searchForm.getBasicSelectedID();
    Connection conn = null;
    //try {
      conn = this.getConnection(request);
      DocumentOperationManager docOpManager = new DocumentOperationManager(this.getSessionContainer(request), conn);
      DmsDocumentDAObject dmsDocDAO = new DmsDocumentDAObject(this.getSessionContainer(request), conn);

      if (!Utility.isEmpty(idAry)) {
        for (int i = 0; i < idAry.length; i++) {
          DmsDocument dmsDoc = (DmsDocument) dmsDocDAO.getObjectByID(new Integer(idAry[i]));
          docOpManager.deleteDocument(dmsDoc);
        }
      }

      docOpManager.release();
      dmsDocDAO = null;
  }
  
  public void deleteListData2(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws ApplicationException {
    SessionContainer sessionContainer = this.getSessionContainer(request);
    Connection conn = this.getConnection(request);
    DocumentValidateManager docValidateManager = new DocumentValidateManager(sessionContainer, conn);
    DocumentOperationManager docOperationManager = new DocumentOperationManager(sessionContainer, conn);
    DocumentRetrievalManager docRetrievalManager = new DocumentRetrievalManager(sessionContainer, conn);
    DmsDocumentDAObject dmsDocumentDAO = new DmsDocumentDAObject(sessionContainer, conn);
    ApplicationContainer container = (ApplicationContainer)request.getSession().getServletContext().getAttribute(GlobalConstant.APPLICATION_CONTAINER_KEY);
    String[] selectedID = request.getParameterValues("basicSelectedID");
    String ignoreDeleteShortcut = request.getParameter("ignoreDeleteShortcut");
    boolean isIgnoreDeleteShortcut = (ignoreDeleteShortcut!=null && ignoreDeleteShortcut.equals("true")) ? true : false;
    //step 1: loop and validate tree node, add id to lstIdHasRight/lstIdMisRight/lstIdBeLocked
    for (int n=0; n<selectedID.length; n++) {
      Integer srcDocumentId = TextUtility.parseIntegerObj(selectedID[n]);
      DmsDocument sourceDocument = (DmsDocument)dmsDocumentDAO.getObjectByID(srcDocumentId);
      DmsValidation validation = docValidateManager.validateDeleteAction(sourceDocument, isIgnoreDeleteShortcut);
      
      //step 2: begin delete documents
      String cacheKey = "D"+sessionContainer.getSessionID()+""+System.currentTimeMillis();
      try {
        List deleteDocumentIds = validation.getLstIdAccept();
        //step 2.1: check and lock operation id to application cache
        boolean success = container.checkAndLockDocumentOperationID(cacheKey, deleteDocumentIds, "ACDIMU");
        if (!success) {
          this.addError(request, "errors.dms.fail_to_delete", docRetrievalManager.getLocationPath(srcDocumentId));
          this.addError(request, "errors.dms.cannot_edit_now");
        }else {
//        step 2.2: delete documents
          docOperationManager.deleteDocument(sourceDocument, validation);
        
          //step 2.3: message throws
          List lstIdHasRight = validation.getLstIdAccept();
          List lstIdMisRight = validation.getLstIdReject();
          List lstIdBeLocked = validation.getLstIdlocked();
          List lstIdBeDeleted = validation.getLstIdDeleted();
          List lstIdBeArchived = validation.getLstIdArchived();
          List lstIdRelatedShortcut = validation.getLstIdHaveRelationship();
          String locationPath = docRetrievalManager.getLocationPath(srcDocumentId);
          if (validation.isSuccess()) {
            this.addMessage(request, "dms.message.document_deleted_success", locationPath, String.valueOf(lstIdHasRight.size()));
          }else {
            this.addError(request, "errors.dms.fail_to_delete", locationPath);
            for (int i=0; i<lstIdMisRight.size(); i++) {
              this.addError(request,"errors.dms.no_permission",docRetrievalManager.getLocationPath((Integer)lstIdMisRight.get(i)));                            
            }
            for (int i=0; i<lstIdBeLocked.size(); i++) {
              this.addError(request,"errors.dms.been_checkout",docRetrievalManager.getLocationPath((Integer)lstIdBeLocked.get(i)));                            
            }
            for (int i=0; i<lstIdBeDeleted.size(); i++) {
              this.addError(request,"errors.dms.been_deleted",docRetrievalManager.getLocationPath((Integer)lstIdBeDeleted.get(i)));                            
            }
            for (int i=0; i<lstIdBeArchived.size(); i++) {
              this.addError(request,"errors.dms.been_archived",docRetrievalManager.getLocationPath((Integer)lstIdBeArchived.get(i)));                            
            }
            for (int i=0; i<lstIdRelatedShortcut.size(); i++) {
              this.addError(request,"errors.dms.has_relationship",docRetrievalManager.getLocationPath((Integer)lstIdRelatedShortcut.get(i)));                            
            }
          }
          
          //step 2.4: delete from clipboard if it present
          List clipboardList = (List)request.getSession().getAttribute("DMS_CLIPBOARD");
          if (clipboardList!=null && !clipboardList.isEmpty()) {
            for (int i=0; i<clipboardList.size(); i++) {
              if(deleteDocumentIds.contains((Integer)clipboardList.get(i))) {
                clipboardList.remove(i--);
              }
            }
          }
        }
      }catch (ApplicationException ex) {
        throw ex;
      }catch (Exception ex) {
        throw new ApplicationException(ex);
      }finally {
        container.unlockDmsDocumentOperationID(cacheKey);
      }
    }
    conn = null;
    sessionContainer = null;
    container = null;
    docValidateManager.release();
    docOperationManager.release();
    docRetrievalManager.release();
  }

  /**
   * execute - refresh form data.
   *
   * @param mapping
   * @param form
   * @param request
   * @param response
   * @return
   * @throws ServletException
   */
  public void refreshFormData(ActionMapping mapping, AbstractActionForm form, HttpServletRequest request, HttpServletResponse response) throws ApplicationException {
    MaintDmsDocumentForm documentForm = (MaintDmsDocumentForm) form;
    DmsDocument document = (DmsDocument) documentForm.getFormData();
    SessionContainer sessionContainer = this.getSessionContainer(request);
    Connection conn = this.getConnection(request);
    DocumentRetrievalManager docRetrievalManager = new DocumentRetrievalManager(sessionContainer, conn);
    
    String docIDStr = documentForm.getID();
    if (Utility.isEmpty(docIDStr)){
      docIDStr = (String)request.getAttribute("documentID");
    }
    if (!Utility.isEmpty(docIDStr)){
     
      Integer docID = new Integer (docIDStr);
      document = docRetrievalManager.getDocument(docID);

      super.refreshFormData(mapping, documentForm, request, response);
      
      if (DmsDocument.COMPOUND_DOC_TYPE.equals(document.getDocumentType())||DmsDocument.PAPER_DOC_TYPE.equals(document.getDocumentType())){
        selectChildList(document.getID(), document.getRootID(), request,form);
      }else{
        selectChildList(document.getParentID(), document.getRootID(), request,form);
      } 
    }

    List udfDetailList = docRetrievalManager.getUDFDetailList(TextUtility.parseIntegerObj(documentForm.getUserDefinedFieldID()));
    documentForm.setUdfDetailList(udfDetailList);
    log.debug("======refresh form data ar list size:"+udfDetailList.size());
    String path = (String)request.getAttribute("location");
    if (Utility.isEmpty(path)) {
      path = docRetrievalManager.getLocationPath(TextUtility.parseIntegerObj(documentForm.getParentID()));
    }
    request.setAttribute("location", path);
    document.setUserDefinedFieldID(TextUtility.parseIntegerObj(documentForm.getUserDefinedFieldID()));
    documentForm.setFormData(document);     
    sessionContainer = null;
    conn=null;
    docRetrievalManager.release();
  }
  
}
